<extend name="Layout/ins_popup" />
<block name="content">
	<div class="popup-frame">
		<div class="popup-header">
			<div class="pull-left">
				<label>
					<input type="radio" id="rb_dept" name="type" value="dept" >
					<span class="lbl">部门</span></label>
				<label>
					<input type="radio" id="rb_position" name="type" value="position">
					<span class="lbl">职位</span> </label>
			</div>
			<div class="pull-right">
				<a class="btn btn_save" id="btn_save">确定</a>
				<a class="btn btn_close btn-cancel" id="btn_close">关闭</a>
			</div>
		</div>
		<div class="popup-body">
			<div class="popup-body-scroll">
				<div class="popup-content">
					<div class="col-23 pull-left">
						<b class="popup-label">地址簿</b>
						<div class="popup_tree_menu">
							<div id="dept" class="" style="height:369px;">
								{$list_dept}
							</div>
							<div id="position" class="hidden" style="height:369px;">
								{$list_position}
							</div>
						</div>
					</div>
					<div class="col-30 pull-right">
						<div class="col-7 pull-left text-center mid" style="margin-top: 24px;"></div>
						<div class="col-23 pull-right">
							<b class="popup-label">人员</b>
							<div id="addr_list" style="width:100%;height:369px;"></div>
						</div>
					</div>
				</div>
			</div>

		</div>
</block>
<block name="js">

	<script type="text/javascript">
		var index = parent.layer.getFrameIndex(window.name);
		layui.use('global', function() {
			var $ = layui.jquery;
			$("#rb_dept").prop('checked', true);
			// 选择用户默认选择的类型
			$("#dept").removeClass("hidden");

			$("#btn_close").on('click', function() {
				parent.layer.close(index);
			});

			$("#btn_save").on('click', function() {
				if ($('#addr_list input:checked').length > 0) {
					var id = $('#addr_list input:checked').val();
					var name = $('#addr_list input:checked').attr('text');
					$("#audit_name", parent.document).val(name);
					$("#audit_id", parent.document).val(id);
				} else {
					layer.msg('请选择人员');
					return false;
				}
				parent.layer.close(index);
			});

			$(document).ready(function() {
				$("input[name='type']").on('click', function() {
					$("input[name='type']").each(function() {
						$("#" + $(this).val()).addClass("hidden");
					});
					$("#" + $(this).val()).removeClass("hidden");
				});
				//点击节点时读取相应的数据
				$(".tree_menu  a").click(function() {
					$(".tree_menu a").removeClass("active");
					var type = $("input[name='type']:checked").val();
					$(this).addClass("active");
					send_ajax("{:U('Popup/read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
						showdata(data);
					});
					return false;
					//禁止连接生效
				});

				// 双击添加到收件人 因后添加的数据 所以需要用live函数
				$(document).on("dblclick", "#addr_list label", function() {
					$text = $(this).text();
					$val = $(this).find("input").val();
					if ($("#rc select option[value='" + $val + "']").val() == undefined) {
						$("<option></option>").val($val).text($text).appendTo("#rc select");
						$("#rc_count").text("(" + $("#rc select option").length + ")");
					};
				});

				/* 双击添加到收件人 因后添加的数据 所以需要用live函数 */
				$("#rc select").on("dblclick", function() {
					$(this).find("option:selected").remove();
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				});

				$("#cc select").on("dblclick", function() {
					$(this).find("option:selected").remove();
					$("#cc_count").text("(" + $("#cc select option").length + ")");
				});

				$("#bcc select").on("dblclick", function() {
					$(this).find("option:selected").remove();
					$("#bcc_count").text("(" + $("#bcc select option").length + ")");
				});
			});
		});

		function showdata(result) {
			$("#addr_list").html("");
			for (s in result.data) {
				var id = result.data[s].id;
				var emp_no = result.data[s].emp_no;
				var position_name = result.data[s].position_name;
				var name = result.data[s].name;
				var name = name + "/" + position_name;
				var html_string = conv_address_item_radio(name, id);
				$("#addr_list").append(html_string);
			}
		}

		//最终确认
		function add_address(name) {
			$("input:checked[name='addr_id']").each(function() {
				$(this).prop('checked', false);
				$text = $(this).parents("label").find("span").text().trim();
				$val = $(this).val();
				if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
					$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				};
			});
		}

		//-->
	</script>
</block>